﻿using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;
using System.Diagnostics.CodeAnalysis;

namespace BBCore.Server.Components.Pages.Admins
{
    /// <summary>
    /// 用户管理界面Table模板
    /// </summary>
    /// <typeparam name="TItem">模型</typeparam>
    [CascadingTypeParameter(nameof(TItem))]
    public partial class EasyAdminTable<TItem> where TItem : class, new()
    {
        private Table<TItem>? _table;

        [NotNull]
        [Parameter]
        public RenderFragment<TItem>? TableColumns { get; set; }

        [Parameter]
        public Func<TItem, ItemChangedType, Task<bool>>? OnSaveAsync { get; set; }

        [Parameter] public Func<IEnumerable<TItem>, Task<bool>>? OnDeleteAsync { get; set; }

        [Parameter] public bool IsTree { get; set; }
        [Parameter] public bool ShowRefresh { get; set; }
        [Parameter] public bool ShowExportButton { get; set; }
        /// <summary>
        /// <para>没下载完再点导出会直接报错,理由是首次使用时会下载 Chromium 运行时，网络不好的情况下可能会很慢</para>
        /// <para>导出PDF首次下次时会先下载(自动解压删除)chrome-win64.zip再chrome-headless-shell-win64.zip</para>
        /// <para>位于bin\Debug\net8.0\Chrome\Win64-128.0.6613.119\chrome-win64</para>
        /// <para>位于bin\Debug\net8.0\ChromeHeadlessShell\Win64-128.0.6613.119\chrome-headless-shell-win64</para>
        /// <para>命令提示输出那里也能看到Fail下载失败的信息,</para> 
        /// </summary>
        [Parameter] public bool ShowExportPdfButton { get; set; }
      
        [Parameter] public RenderFragment<ITableExportContext<TItem>>? ExportButtonDropdownTemplate {  get; set; }

        [Parameter] public Func<IEnumerable<TItem>, Task<IEnumerable<TableTreeNode<TItem>>>>? TreeNodeConverter { get; set; }

        [Parameter] public Func<TItem, bool>? ShowExtendEditButtonCallback { get; set; }

        [Parameter] public Func<TItem, bool>? ShowExtendDeleteButtonCallback { get; set; }
        [Parameter] public Func<TItem, bool>? ShowDetailRow {  get; set; }

        [Parameter] public RenderFragment<TItem>? BeforeRowButtonTemplate { get; set; }

        [Parameter] public RenderFragment? TableToolbarTemplate { get; set; }

        [Parameter] public RenderFragment? TableExtensionToolbarTemplate { get; set; }
        [Parameter] public RenderFragment<TItem>? DetailRowTemplate { get; set; }

        [Parameter] public RenderFragment<TItem>? RowButtonTemplate { get; set; }

        [Parameter] public bool IsPagination { get; set; } = true;

        [Parameter] public bool IsMultipleSelect { get; set; } = false;
        


        public async Task QueryAsync()
        {
            //我们可以使用@ref指令，将当前Table对象引用到一个变量上面,通过下面的代码在外部调用Table的QueryAsync刷新数据
            //Table组件的Items和OnQueryAsync只能二选一，不能混用
            await _table!.QueryAsync();
        }

    }
}
